package com.ruoyi.data.opc;

import com.ruoyi.data.opc.config.Client;
import com.ruoyi.machine.advanced.alarm.service.IMachineAlarmService;
import com.ruoyi.machine.advanced.status.service.IStatusService;
import com.ruoyi.machine.base.device.service.IDeviceService;
import com.ruoyi.produce.base.shift.service.IProduceShiftInfoService;
import com.ruoyi.produce.process.datacollection.service.IDataCollectionService;
import com.ruoyi.produce.process.parameter.service.IProcessParameterService;
import com.ruoyi.produce.process.scheduling.service.ISchedulingService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.util.concurrent.TimeUnit;

/**
 * 读取数据测试
 */
@Component
@Controller
@Order(value = 1)
@Api(value = "测试读取数据模块", tags = "测试读取数据接口")
public class OpcUAController implements CommandLineRunner {
    @Autowired
    private IStatusService statusService;
    @Autowired
    private ISchedulingService schedulingService;
    @Autowired
    private IDataCollectionService collectionService;
    @Autowired
    private IProduceShiftInfoService produceShiftInfoService;
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private IMachineAlarmService machineAlarmService;
    @Autowired
    private IProcessParameterService processParameterService;

    /**
     * 获取数据，保存到数据库中(x分钟调度一次)
     */
    public void searchData() {
        System.out.println("opc开始测试");
        Thread thread = new Thread(() -> {
            while (true) {
                System.out.println("opc设备开始读数据");
                Client.onLock();
                OpcUAClientRunner.collectData(statusService, schedulingService, collectionService, produceShiftInfoService, deviceService, machineAlarmService, processParameterService);
                Client.unLock();
                try {
//                    TimeUnit.SECONDS.sleep(60000);
                    Thread.sleep(30000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }

    @Override
    public void run(String... args) throws Exception {
//        searchData();
    }
}
